package com.qianfeng.xiaxk.activity;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.easemob.EMCallBack;
import com.easemob.chat.EMChatManager;
import com.easemob.chat.EMGroupManager;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.qianfeng.xiaxk.R;
import com.qianfeng.xiaxk.utils.LoginUtils;

import java.util.HashMap;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.sina.weibo.SinaWeibo;

@ContentView(R.layout.activity_login)
public class LoginActivity extends AppCompatActivity implements PlatformActionListener {
    @ViewInject(R.id.login_phone)
    TextView loginPhone;
    @ViewInject(R.id.login_password)
    TextView loginPassword;
    @ViewInject(R.id.forget_password)
    TextView forgetPassword;
    @ViewInject(R.id.login_btn)
    Button loginBtn;
    @ViewInject(R.id.weibo_login)
    ImageView weiboLogin;
    @ViewInject(R.id.wechat_login)
    ImageView wechatLogin;
    @ViewInject(R.id.qq_login)
    ImageView qqLogin;

    ProgressDialog progressDialog;
    boolean isFirstCome = true;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ViewUtils.inject(this);
    }

    @OnClick({R.id.login_btn, R.id.weibo_login, R.id.wechat_login, R.id.qq_login, R.id.forget_password})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.login_btn:////用户输入了账号密码，且点击了登陆
                break;
            case R.id.weibo_login:///用微博登陆
                weiboLogin();
                break;
            case R.id.wechat_login:////用户微信登陆
                break;
            case R.id.qq_login:////用qq登陆
                break;
        }
    }

    public void initProgressDialog() {
        //实例化
        progressDialog = new ProgressDialog(this);

        //设置进度条风格，风格为圆形，旋转的
        progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);///圆的

        //设置ProgressDialog 标题
        progressDialog.setTitle("授权提示");

        //设置ProgressDialog 提示信息
        progressDialog.setMessage("授权中，请耐心等待结果...");

        //设置ProgressDialog 标题图标 //progressDialog.setIcon(R.drawable.android);

        //设置ProgressDialog 的进度条是否不明确
        progressDialog.setIndeterminate(false);

        //设置ProgressDialog 是否可以按退回按键取消
        progressDialog.setCancelable(false);

    }

    ////用户点击了微博登陆
    private void weiboLogin() {
        Platform weibo = ShareSDK.getPlatform(this, SinaWeibo.NAME);
        Log.e("","---isAuthValid:"+weibo.isAuthValid()+"----isValid-"+weibo.isValid());
        /*Log.e("weibologin","--weibologin");

        if(weibo.isAuthValid()){
            weibo.removeAccount();
        }else {*/
            weibo.setPlatformActionListener(this);
            weibo.SSOSetting(false);   //优先使用客户端登陆，为true时使用网页登陆
            weibo.showUser(null);
            isFirstCome = false;
        //}
    }

    @Override
    public void onComplete(Platform platform, int i, HashMap<String, Object> res) {
        Log.e("","---loginActivity-onComplete");
        if (platform instanceof SinaWeibo) {
            Intent intent = new Intent();
            intent.putExtra("id", res.get("id").toString());
            intent.putExtra("name", res.get("name").toString());
            intent.putExtra("icon", res.get("profile_image_url").toString());
            //intent.putExtra("sex",res.get("sex").toString());
            //intent.putExtra("state", res.get("state").toString());
            setResult(Activity.RESULT_OK, intent);

            progressDialog.dismiss();
            EMChatManager.getInstance().login(res.get("name").toString(),res.get("id").toString(),new EMCallBack() {//回调
                @Override
                public void onSuccess() {
                    runOnUiThread(new Runnable() {
                        public void run() {
                            EMGroupManager.getInstance().loadAllGroups();
                            EMChatManager.getInstance().loadAllConversations();
                            Log.d("main", "登陆聊天服务器成功！");
                        }
                    });
                }

                @Override
                public void onProgress(int progress, String status) {

                }

                @Override
                public void onError(int code, String message) {
                    Log.d("main", "登陆聊天服务器失败！");
                }
            });

            this.finish();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if(progressDialog==null) {
            initProgressDialog();
        }
        if(!isFirstCome){
            progressDialog.show();
        }

    }

    @Override
    public void onError(Platform platform, int i, Throwable throwable) {
        Intent intent = new Intent();
        setResult(RESULT_CANCELED, intent);
        this.finish();
    }

    @Override
    public void onCancel(Platform platform, int i) {
        Intent intent = new Intent();
        setResult(RESULT_CANCELED, intent);
        this.finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        progressDialog.dismiss();
    }
}
